package com.simple.cms.model.mapper;

import com.simple.cms.model.request.ProductForm;
import com.simple.cms.model.response.ProductBaseInfo;
import com.simple.cms.repository.entity.Product;
import org.apache.commons.collections4.CollectionUtils;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.modelmapper.TypeToken;

import java.util.Collections;
import java.util.List;

/**
 * @author kin
 * @since 2017/7/19
 */
public class ProductConverter extends BaseConverter {

    public static Product converterToDo(ProductForm productForm) {
        if (productForm == null) {
            return null;
        }

        ModelMapper modelMapper = new ModelMapper();
        return modelMapper.map(productForm, new TypeToken<Product>() {
        }.getType());
    }


    public static List<ProductBaseInfo> converterToInfoList(List<Product> productList) {
        if (CollectionUtils.isEmpty(productList)) {
            return Collections.emptyList();
        }

        ModelMapper modelMapper = new ModelMapper();
        modelMapper.addMappings(new PropertyMap<Product, ProductBaseInfo>() {
            protected void configure() {
                map(source.getPrice().toString(), destination.getPrice());
                using(DateToString).map(source.getCreateDate(), destination.getCreateDate());
            }
        });
        return modelMapper.map(productList, new TypeToken<List<ProductBaseInfo>>() {
        }.getType());
    }
}
